//
//  PywPayPwdSetProblemViewCell.m
//  PywSdk
//
//  Created by smallwave on 16/8/17.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywPayPwdSetProblemViewCell.h"
#import "UILabel+Pyw.h"
#import "PywCommonManager.h"
#import "PywDatePickerView.h"
#import "PywMasonry.h"

@interface PywPayPwdSetProblemViewCell ()<UITextFieldDelegate, PywDatePickerViewDelegate>

@property (nonatomic, strong) UILabel               *problemLabel;  // 问题Labe
@property (nonatomic, strong) UITextField           *answerText;    // 答案Text
@property (nonatomic, strong) UIView                *bottomLine;    // 底部分隔线
@property (nonatomic, strong) PywDatePickerView     *datePicker;    // 日期选择器

@end

@implementation PywPayPwdSetProblemViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUp];
    }
    return self;
}
#pragma mark - PrepareUI
- (void)setUp{
    self.backgroundColor = [UIColor clearColor];
    self.userInteractionEnabled = YES;
    
    [self addSubview:self.problemLabel];
    [self addSubview:self.answerText];
    [self addSubview:self.bottomLine];
}
- (void)layoutSubviews{
    __weak typeof(self) wSelf = self;
    [self.problemLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerY.equalTo(wSelf.answerText.mas_centerY);
        make.top.equalTo(wSelf);
        make.left.equalTo(wSelf.mas_left).offset(10);
        make.width.equalTo(@100);
    }];
    [self.answerText mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.right.equalTo(wSelf);
        make.left.equalTo(wSelf.problemLabel.mas_right);
    }];
    [self.bottomLine mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.equalTo(wSelf.mas_left).offset(15);
        make.right.equalTo(wSelf.mas_right).offset(-15);
        make.top.equalTo(wSelf.answerText.mas_bottom);
        make.bottom.equalTo(wSelf.mas_bottom);
        make.height.equalTo(@1);
    }];
}
#pragma mark - Action
- (void)removeDatePicker{
    [self.datePicker hidden];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self removeDatePicker];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (self.answerIsDatePicker == YES) {
        
        [[UIApplication sharedApplication].keyWindow addSubview:self.datePicker];
        __weak typeof(self) wSelf = self;
        [self.datePicker mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.equalTo(wSelf.answerText.mas_bottom);
            make.width.equalTo(@260);
            make.height.equalTo(@200);
        }];
        
//        if ([self.delegate respondsToSelector:@selector(endEditing)]) {
//            [self.delegate endEditing];
//        }
        
        self.answerText.tintColor = [UIColor clearColor]; // 去除光标显示
        return YES;
    } else {
        
//        if ([self.delegate respondsToSelector:@selector(hiddenDatePicker)]) {
//            [self.delegate hiddenDatePicker];
//        }
        return YES;
    }
}

#pragma mark - PywDatePickerViewDelegate
- (void)datePickerView:(PywDatePickerView *)datePickerView didCommitWithSelectDate:(NSString *)date{
    self.answerText.text = date;
    [self removeDatePicker];
}
#pragma mark - Lazy
- (UILabel *)problemLabel{
    if (!_problemLabel) {
        _problemLabel = [UILabel labelWithText:nil
                                 textAlignment:NSTextAlignmentCenter
                                      fontSize:14
                                     textColor:[UIColor blackColor]];
        _problemLabel.text = self.problem;
    }
    return _problemLabel;
}
- (UITextField *)answerText{
    if (!_answerText) {
        CGFloat fontSize = [[PywCommonManager manager] changeDeviceTextFontSize:14];
        _answerText = [[UITextField alloc] init];
        _answerText.backgroundColor = [UIColor clearColor];
        _answerText.font = [UIFont systemFontOfSize:fontSize];
        _answerText.delegate = self;
    }
    return _answerText;
}
- (UIView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = [UIColor lightGrayColor];
    }
    return _bottomLine;
}
- (PywDatePickerView *)datePicker{
    if (!_datePicker) {
        _datePicker = [[PywDatePickerView alloc] initWithDelegate:self];
    }
    return _datePicker;
}
#pragma mark - Setter & Getter
- (void)setProblem:(NSString *)problem{
    _problem = problem;
    self.problemLabel.text = _problem;
}
- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.answerText.placeholder = _placeholder;
}
- (void)setAnswer:(NSString *)answer{
    _answerText.text = answer;  // 设置切换时，清空text显示
}
- (NSString *)answer{
    return self.answerText.text;
}

@end
